<?php

	class AnalysisFilter implements IFilter
	{				
		public function init($data)
		{
			
		}
	
		public function doFilter($request,$response)
		{
			if ($request->containsData('__ro_id'))
			{
				if (is_null($response))
				{				
					
				}
				else
				{
					$y = Date('Y');
					$m = Date('m');
					$d = Date('d');
					$h = Date('H');
					$url = $request->url;
					$ref = $request->referer;
					$ro_id = $request->getData('__ro_id');
									
					$hits_id = EbModel::getModel('Analysis','SiteHits')->insert((object)array('year' => $y,'month' => $m,'day' => $d,'hour' => $h,'url' => $url,'ro_id' => $ro_id,'generated_time' => round(EbDebug::$generatedTime*1000),'memory_used' => round((EbDebug::$afterMemory - EbDebug::$beforeMemory) / 1024)));
					
					$session = EbController::$session;
					
					if (is_null($session->getData('analysis_checked')))
					{
						$session->setData('analysis_checked',1);
						
						$referer_model = EbModel::getModel('Analysis','Referer');
						
						$referer = $referer_model->selectByUrl_1($request->referer);
						
						if (is_null($referer))
						{
							$referer_id = $referer_model->insert((object)array('url' => is_null($request->referer) ? '' : $request->referer,'hits' => 1));
						}
						else
						{
							$referer_id = $referer->referer_id;
							
							$referer_model->update((object)array('referer_id' => $referer_id,'hits' => $referer->hits + 1));
						}
						
						EbModel::getModel('Analysis','EntryPoint')->insert((object) array('referer_id' => $referer_id,'hits_id' => $hits_id));
					}
				}
			}
		}
	
	}